package com.monster.servlet.detail;

import com.monster.pojo.Album;
import com.monster.pojo.Singer;
import com.monster.pojo.Song;
import com.monster.service.ListServiceImpl;
import com.monster.service.SingerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/GetSingerDetaiServlet")
public class GetSingerDetaiServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        switch (opr) {
            case "showDetail":
                showDetail(request,response);
                break;
        }
    }

    private void showDetail(HttpServletRequest request, HttpServletResponse response){
        String no = request.getParameter("singer_id");
        List<Album> albumList = new ArrayList<>();
        ArrayList<Song> songList = new ArrayList<>();
        Singer singer_info = new Singer();
        Long singerId = Long.parseLong(no);
        SingerService singerService = new ListServiceImpl();
        singer_info = singerService.getSingersBySingerId(singerId);
        albumList = singerService.selectAlbumBySingerId(singerId);
        songList = singerService.getSongsBySingerId(singerId);
        if(singer_info!=null){
            request.setAttribute("singer_info", singer_info);
        }

        if(albumList!=null) {
            int countAlbums = albumList.size();
            request.setAttribute("aList", albumList);
            request.setAttribute("countAlbums",countAlbums);
        }else{
            int countAlbums = 0;
            request.setAttribute("countAlbums",countAlbums);
        }

        if(songList!=null){
            int countSongs = songList.size();
            request.setAttribute("songList",songList);
            request.setAttribute("countSongs",countSongs);
        }else{
            int countSongs = 0;
            request.setAttribute("countSongs",countSongs);
        }
        try {
            request.getRequestDispatcher("singer-detail.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
